﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QldtSdh.BLL;
using QldtSdh.DAL.Models;
using QldtSdh.GUI.Utils;

namespace QldtSdh.GUI.DuLieu
{
    public partial class FormQuanLyPhongHoc : QldtSdh.GUI.Common.BaseDialog
    {
        bool allowUpdate = false;
        public FormQuanLyPhongHoc()
        {
            InitializeComponent();
            gridData.AddRowIndexing();
            InitGrid();
            allowUpdate = false;
            if (LoadCoSoDaoTao())
                LoadGrid();
            allowUpdate = true;
        }

        bool LoadCoSoDaoTao()
        {
            var ls = BizCoSoDaoTao.GetList();
            cmbCoSoDaoTao.DataSource = ls;
            cmbCoSoDaoTao.DisplayMember = "TenCoSoDaoTao";
            cmbCoSoDaoTao.ValueMember = "MaCoSoDaoTaoGUID";
            if (ls == null || ls.Count == 0)
            {
                btnThem.Enabled = btnSua.Enabled = btnXoa.Enabled = false;
                return false;
            }
            else return true;
        }

        void InitGrid()
        {
            gridData.AutoGenerateColumns = false;
            colTenPhongHoc.DataPropertyName = "TenPhongHoc";
            colSoLuongHocVienHocToiDa.DataPropertyName = "SoLuongHocToiDa";
            colSoLuongHocVienThiToiDa.DataPropertyName = "SoLuongThiToiDa";
        }

        void LoadGrid()
        {
            if (btnThem.Enabled)
            {
                bdData.DataSource = BizPhongHoc.GetList((cmbCoSoDaoTao.SelectedItem as CoSoDaoTao).MaCoSoDaoTaoGUID);
                gridData.DataSource = bdData;
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            var current = bdData.Current as PhongHoc;
            if (current != null)
            {
                if (DialogUtils.ShowOKCancel(NOTIFY_CONSTANT_STRING.DELETE) == System.Windows.Forms.DialogResult.OK)
                {
                    var rs = BizPhongHoc.Delete(current);
                    if (rs == CODE_RESULT_RETURN.ThanhCong)
                        bdData.RemoveCurrent();
                    else if (rs == CODE_RESULT_RETURN.ThatBai)
                        DialogUtils.ShowError(string.Format(ERROR_CONSTANT_STRING.LOI_XOA, ENTITY_NAME.PHONGHOC));
                }
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            var frm = new DuLieu.FormQuanLyPhongHoc_ChiTiet(cmbCoSoDaoTao.SelectedItem as CoSoDaoTao);
            frm.OnChanged += (o, t) =>
            {
                LoadGrid();
            };
            frm.ShowDialog();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            var current = bdData.Current as PhongHoc;
            if (current != null)
            {
                var frm = new DuLieu.FormQuanLyPhongHoc_ChiTiet(cmbCoSoDaoTao.SelectedItem as CoSoDaoTao, current);
                if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    LoadGrid();
            }
        }

        private void bdData_CurrentChanged(object sender, EventArgs e)
        {
            var current = bdData.Current as PhongHoc;
            if (current != null)
            {
                btnSua.Enabled = btnXoa.Enabled = true;
            }
            else
            {
                btnSua.Enabled = btnXoa.Enabled = false;
            }
        }

        private void cmbCoSoDaoTao_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (allowUpdate)
            {
                LoadGrid();
            }
        }
    }
}
